package com.oa.lms.apps.dao.limit.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.lms.apps.dao.limit.ILimitInfoDao;
import com.oa.lms.apps.entity.limit.LimitInfoValue;

public class LimitInfoDaoImpl extends HibernateDaoSupport implements
		ILimitInfoDao {

	private List<LimitInfoValue> list;

	// DAO method
	@Override
	public LimitInfoValue queryByLimitId(Integer limitId)
			throws Exception {
		if (limitId != null) {
			return (LimitInfoValue) this.getHibernateTemplate().get(LimitInfoValue.class, limitId);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LimitInfoValue> queryByParentId(Integer parentId, String limitIds)
			throws Exception {
		if (parentId != null && limitIds != null) {
			if (parentId == 0) {
				return this.getHibernateTemplate().find("FROM LimitInfoValue liv WHERE liv.states=1 AND liv.parentId=? AND liv.limitId in ("+ limitIds +") AND (SELECT count(lifv.limitId) FROM LimitInfoValue lifv WHERE lifv.parentId = liv.limitId) > 0", parentId);
			}
			return this.getHibernateTemplate().find("FROM LimitInfoValue liv WHERE liv.parentId = ? AND liv.states=1 AND liv.limitId in ("+ limitIds +")", parentId);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<LimitInfoValue> queryByLimitIds(String limitIds)
			throws Exception {
		String hql = " FROM LimitInfoValue liv WHERE liv.limitId in ("+limitIds+") AND (SELECT count(liv1.limitId) FROM LimitInfoValue liv1 WHERE liv1.parentId = liv.limitId) = 0 AND liv.parentId != 0";
		return this.getHibernateTemplate().find(hql);
	}

	public List<LimitInfoValue> getList() {
		return list;
	}

	public void setList(List<LimitInfoValue> list) {
		this.list = list;
	}
	
}
